package concurrency2;

public class WaitAndNotifyDemo {

    private static Object lock = new Object();

    public static void main(String[] args) {
        Thread a = new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 20; i++) {
                    try {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notifyAll();
            }
        }, "A");

        a.start();

        Thread b = new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 20; i++) {
                    try {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                        lock.notify();
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notifyAll();
            }
        }, "B");

        b.start();
    }

}
